const { PIE } = window;

/** PIEGeometry 转 geojson **/
export function toGeojson(drawGeo: any) {
  let feature = {
    type: "Feature",
    geometry: {
      coordinates: [],
    },
    properties: {},
  };
  let coordinates: any = [];
  if (drawGeo) {
    var geometry = {
      type: "",
      coordinates: [],
    };
    if (drawGeo instanceof PIE.Point) {
      geometry.type = "Point";
      var pointGeo = drawGeo;
      var point = pointGeo.getPoint();
      coordinates.push(point.x);
      coordinates.push(point.y);
      geometry.coordinates = coordinates;
    } else if (drawGeo instanceof PIE.Polyline) {
      geometry.type = "LineString";
      var polyLine = drawGeo;
      var subCount = polyLine.getSubCount();
      for (var subIndex = 0; subIndex < subCount; subIndex++) {
        let points = polyLine.getPoints(subIndex);
        for (let pnt of points) {
          let { x, y } = pnt;
          coordinates.push([x, y]);
        }
      }
      geometry.coordinates = coordinates;
    } else if (drawGeo instanceof PIE.Polygon) {
      geometry.type = "Polygon";
      var polygon = drawGeo;
      var subCount = polygon.getSubCount();
      for (var subIndex = 0; subIndex < subCount; subIndex++) {
        var subPoints = [];
        var points = polygon.getPoints(subIndex);
        for (let pnt of points) {
          let { x, y } = pnt;
          subPoints.push([x, y]);
        }
        coordinates.push(subPoints);
      }
      geometry.coordinates = coordinates;
    } else {
      geometry.type = "UnKnow";
    }
    feature.geometry = geometry;
  }
  return feature;
}
